import json
import time

from mqtt.common.common_topic_request import CommonTopicRequest


class MethodTopicRequest(CommonTopicRequest):
    def __init__(self, tid=None, bid=None, timestamp=None, data=None, method=None):
        # 调用父类的构造函数
        super().__init__(tid, bid, timestamp, data)
        self._method = method

    def __str__(self):
        # 调用父类的 __str__ 方法并增加 method 属性
        return f"{super().__str__()}, method='{self._method}'"

    # Getter and Setter for method
    @property
    def method(self):
        return self._method

    @method.setter
    def method(self, method):
        self._method = method

    def to_json(self):
        return json.dumps({
            "tid": self._tid,
            "bid": self._bid,
            "timestamp": int(time.time() * 1000),
            "data": self._data,
            "method": self._method
        })
